package com.lidroid.xutils.view.event;

import ohos.agp.components.Component;
import ohos.agp.components.Component.ClickedListener;
import ohos.agp.components.Component.LongClickedListener;
import ohos.agp.components.ListContainer;
import ohos.agp.components.ListContainer.ItemClickedListener;
import ohos.agp.components.ListContainer.ItemLongClickedListener;
import ohos.agp.components.ListContainer.ItemSelectedListener;

import java.lang.reflect.Method;

public class ViewCommonEventListener implements ClickedListener, LongClickedListener, ItemClickedListener, ItemSelectedListener, ItemLongClickedListener {

    private Object handler;

    private String clickMethod;
    private String longClickMethod;
    private String itemClickMethod;
    private String itemLongClickMethod;
    private String itemSelectMethod;
    private String nothingSelectedMethod;

    public ViewCommonEventListener(Object handler) {
        this.handler = handler;
    }


    public ViewCommonEventListener click(String method) {
        this.clickMethod = method;
        return this;
    }

    public ViewCommonEventListener longClick(String method) {
        this.longClickMethod = method;
        return this;
    }

    public ViewCommonEventListener itemClick(String method) {
        this.itemClickMethod = method;
        return this;
    }

    public ViewCommonEventListener itemLongClick(String method) {
        this.itemLongClickMethod = method;
        return this;
    }

    public ViewCommonEventListener select(String method) {
        this.itemSelectMethod = method;
        return this;
    }

    public ViewCommonEventListener noSelect(String method) {
        this.nothingSelectedMethod = method;
        return this;
    }


    public void onClick(Component v) {
        invokeClickMethod(handler, clickMethod, v);
    }

    public boolean onLongClick(Component v) {
        return invokeLongClickMethod(handler, longClickMethod, v);
    }

    public void onItemClick(ListContainer parent, Component view, int position, long id) {
        invokeItemClickMethod(handler, itemClickMethod, parent, view, position, id);
    }

    public boolean onItemLongClick(ListContainer parent, Component view, int position, long id) {
        return invokeItemLongClickMethod(handler, itemLongClickMethod, parent, view, position, id);
    }

    public void onItemSelected(ListContainer parent, Component view, int position, long id) {
        invokeItemSelectMethod(handler, itemSelectMethod, parent, view, position, id);
    }

    public void onNothingSelected(ListContainer parent) {
        invokeNoSelectMethod(handler, nothingSelectedMethod, parent);
    }


    private static Object invokeClickMethod(Object handler, String methodName, Object... params) {
        if (handler == null) return null;
        Method method = null;
        try {
            // void onClick(View v)
            method = handler.getClass().getDeclaredMethod(methodName, Component.class);

                return method.invoke(handler, params);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    private static boolean invokeLongClickMethod(Object handler, String methodName, Object... params) {
        if (handler == null) return false;
        Method method = null;
        try {
            // boolean onLongClick(View v)
            method = handler.getClass().getDeclaredMethod(methodName, Component.class);

                Object obj = method.invoke(handler, params);
                return obj == null ? false : Boolean.valueOf(obj.toString());

        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }

    private static Object invokeItemClickMethod(Object handler, String methodName, Object... params) {
        if (handler == null) return null;
        Method method = null;
        try {
            // void onItemClick(AdapterView<?> parent, View view, int position, long id)
            method = handler.getClass().getDeclaredMethod(methodName, ListContainer.class, Component.class, int.class, long.class);

            return method.invoke(handler, params);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    private static boolean invokeItemLongClickMethod(Object handler, String methodName, Object... params) {
        if (handler == null) return false;
        Method method = null;
        try {
            // boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id)
            method = handler.getClass().getDeclaredMethod(methodName, ListContainer.class, Component.class, int.class, long.class);

            Object obj = method.invoke(handler, params);
            return Boolean.valueOf(obj == null ? false : Boolean.valueOf(obj.toString()));

        } catch (Exception e) {
            e.printStackTrace();
        }

        return false;
    }

    private static Object invokeItemSelectMethod(Object handler, String methodName, Object... params) {
        if (handler == null) return null;
        Method method = null;
        try {
            // void onItemSelected(AdapterView<?> parent, View view, int position, long id)
            method = handler.getClass().getDeclaredMethod(methodName, ListContainer.class, Component.class, int.class, long.class);

            return method.invoke(handler, params);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    private static Object invokeNoSelectMethod(Object handler, String methodName, Object... params) {
        if (handler == null) return null;
        Method method = null;
        try {
            // void onNothingSelected(AdapterView<?> parent)
            method = handler.getClass().getDeclaredMethod(methodName, ListContainer.class);

            return method.invoke(handler, params);

        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }


    @Override
    public void onLongClicked(Component component) {
        //TODO
    }

    @Override
    public void onItemClicked(ListContainer listContainer, Component component, int i, long l) {
        //TODO

    }

    @Override
    public boolean onItemLongClicked(ListContainer listContainer, Component component, int i, long l) {
        //TODO
        return false;
    }
}
